package com.shop.milk_tea_shop.manager;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shop.milk_tea_shop.dto.MenuDTO;
import com.shop.milk_tea_shop.common.CodeEnum;
import com.shop.milk_tea_shop.common.GlobalInfo;
import com.shop.milk_tea_shop.common.Result;
import com.shop.milk_tea_shop.entity.ItemCategory;
import com.shop.milk_tea_shop.entity.ItemInfo;
import com.shop.milk_tea_shop.service.ItemCategoryService;
import com.shop.milk_tea_shop.service.ItemInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: zrm
 * @CreateTime: 2024/06/23  15:35
 * @Description: 获取菜单信息
 */
@Component
public class MenuManager {
    @Autowired
    private ItemCategoryService itemCategoryService;

    @Autowired
    private ItemInfoService itemInfoService;

    public Result<List<MenuDTO>> getMenu(){
        // 查询所有分类
        List<ItemCategory> list = itemCategoryService.list();
        List<MenuDTO> result = new ArrayList<>();
        // 遍历获取每个分类下的商品list
        list.forEach(itemCategory -> {
            Long categoryId = itemCategory.getCategoryId();
            LambdaQueryWrapper<ItemInfo> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(categoryId!=null, ItemInfo::getCategoryId, categoryId);
            List<ItemInfo> items = itemInfoService.list(queryWrapper);
            MenuDTO element = new MenuDTO(categoryId, itemCategory.getCategoryName(), items);
            result.add(element);
        });
        return new Result<>(CodeEnum.SUCCESS.getMsg(), CodeEnum.SUCCESS, result, new GlobalInfo());
    }

}
